//
//  ProductionViewController.m
//  Khmer media
//
//  Created by Rainseylong on 2/12/13.
//  Copyright (c) 2013 imer. All rights reserved.
//

#import "ProductionViewController.h"
#import "AlbumViewController.h"
#import "PRODUCTION.h"
#import "ViewFactory.h"
#import "DataManager.h"

@interface ProductionViewController ()

@end

@implementation ProductionViewController

@synthesize productionArray;
@synthesize productionTable;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadDataFromServer];
}

-(void)loadDataFromServer
{
    [PRODUCTION convertProductionToOBjectWithBlock:^(NSArray *productionData, NSError *error) {
        
        self.productionArray=productionData;
        [self.productionTable reloadData];
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numRow=self.productionArray.count >0 ? self.productionArray.count : 1;
    return numRow;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[ViewFactory cellOfKind:@"ProductionCell" forTable:tableView];
    UILabel *productionName=(UILabel *)[cell viewWithTag:10];
    
    if (self.productionArray.count ==0)
    {
        productionName.text=@"Loading...";
    }
    else
    {
        PRODUCTION *objProduction=[self.productionArray objectAtIndex:indexPath.row];
        productionName.font=[UIFont fontWithName:@"KhmerOSFasthand" size:15];
        productionName.text=objProduction.productionName;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumViewController *albumView=[[AlbumViewController alloc] initWithNibName:@"AlbumViewController" bundle:NULL];
    
    PRODUCTION *objProduction=[self.productionArray objectAtIndex:indexPath.row];
    albumView.productionId=objProduction.productionId.intValue;
    
    //add productionName
    [DataManager sharedDataManager].productionName=[NSString stringWithFormat:@"%@/",objProduction.productionName];
    [self.navigationController pushViewController:albumView animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
